package com.yd.mvc.mobile.cartController;

import com.yd.app.service.pc.userCartService.UserCartService;
import com.yd.app.util.BusinessException;
import com.yd.domain.business.ShoppingCart;
import com.yd.domain.sys.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by bxd on 2018/3/21.
 */
@RestController
@RequestMapping(value = "/mapi/mshopingcart")
public class MShopingCartController {
    @Autowired
    private UserCartService userCartService;
    /**
     * 添加
     *
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public void addUserCart(@RequestBody ShoppingCart shoppingCart,HttpServletRequest request)throws BusinessException{
        HttpSession session = request.getSession();
        User u = (User) session.getAttribute("login_user");
       if(u!=null){
           shoppingCart.setUserId(u.getId());
           userCartService.addCart(shoppingCart);
        }else{
           throw new BusinessException(401,"尚未登录","no-login");
       }
    }
    /**
     * @Author: zy
     * @Desc: 查询购物车是否存在
     * @Date:  2018/4/8
     * @Param
     */
    @RequestMapping(value = "/getCartByUGId/{goodsId}/{goodsWeightId}", method = RequestMethod.GET)
    public ShoppingCart getCartByUGId(@PathVariable("goodsId") String goodsId,@PathVariable("goodsWeightId") String goodsWeightId,HttpSession session) throws BusinessException {
        User u = (User) session.getAttribute("login_user");
        ShoppingCart shoppingCart;
        if(u!=null){
            shoppingCart=userCartService.getCartByUGId(u.getId(),goodsId,goodsWeightId);
            /*if(shoppingCart==null){
                shoppingCart=new ShoppingCart();
            }*/
        }else{
            throw new BusinessException(401,"尚未登录","no-login");
        }
        return shoppingCart;
    }
    /**
     * 修改
     *
     */
   /* @RequestMapping(value = "/update", method = RequestMethod.POST)
    public void updateUserCart(@RequestBody ShoppingCart shoppingCart) {
        userCartService.updateCart(shoppingCart);
    }*/
    /**
     * 修改
     *
     */
    @RequestMapping(value = "/updateNumber/{goodsId}/{number}", method = RequestMethod.GET)
    public void updateUserCartNumber(@PathVariable("goodsId") String goodsId,@PathVariable("number") int number,HttpSession session) throws BusinessException {
        User u = (User) session.getAttribute("login_user");
        if(u!=null){
            userCartService.updateCartNumber(u.getId(),goodsId,number);
        }else{
            throw new BusinessException(401,"尚未登录","no-login");
        }
    }
    /**
     * 删除
     *
     */
    @RequestMapping(value = "/delete/{goodsId}/{goodsWeightId}", method = RequestMethod.GET)
    public void deleteUserCart( @PathVariable("goodsId") String goodsId,@PathVariable("goodsWeightId") String goodsWeightId,HttpSession session) throws BusinessException {
        User u = (User) session.getAttribute("login_user");
        if(u!=null){
            userCartService.deleteCart(u.getId(), goodsId,goodsWeightId);
        }else{
            throw new BusinessException(401,"尚未登录","no-login");
        }
    }

    /**
     * 查询用户购物车信息
     *
     */
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public List<ShoppingCart> getListCart(HttpSession session) throws BusinessException {
        User u = (User) session.getAttribute("login_user");
        List<ShoppingCart> list=new ArrayList<>();
        if(u!=null){
            list = userCartService.getListCart(u.getId());
        }else{
            throw new BusinessException(401,"尚未登录");
        }
        return list;
    }
}
